<template>
  <div>
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="onCancel"
      @change="onChange"
      :default-index="value"
    />
  </div>
</template>

<script>
import { updateProfile } from '@/api/user'
export default {
  data () {
    return {
      columns: ['男♂', '女♀']
    }
  },
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  created () {

  },

  methods: {
    async onConfirm (value, index) {
      if (this.value === index) return
      try {
        await updateProfile({
          gender: index
        })
        this.$toast('更新成功')
      } catch (err) {
        return this.$toast('更新失败')
      }
      this.$emit('close')
      this.$emit('input', index)
    },
    onChange (picker, value, index) {
      this.$toast(`当前值：${value}, 当前索引：${index}`)
    },
    onCancel () {
      this.$emit('close')
    }
  }
}
</script>

<style scoped lang='less'>

</style>
